{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 064. Minimum Path Sum 最小路径和\n",
    "\n",
    "### 难度：Medium\n",
    "\n",
    "## 刷题内容\n",
    "\n",
    "> 原题链接\n",
    "\n",
    " - 中文：https://leetcode-cn.com/problems/minimum-path-sum\n",
    " - 英文：https://leetcode.com/problems/minimum-path-sum\n",
    " \n",
    "> 内容描述\n",
    "\n",
    "```\n",
    "给定一个包含非负整数的 m x n 网格，请找出一条从左上角到右下角的路径，使得路径上的数字总和为最小。\n",
    "\n",
    "说明：每次只能向下或者向右移动一步。\n",
    "\n",
    "示例:\n",
    "\n",
    "输入:\n",
    "[\n",
    "  [1,3,1],\n",
    "  [1,5,1],\n",
    "  [4,2,1]\n",
    "]\n",
    "输出: 7\n",
    "解释: 因为路径 1→3→1→1→1 的总和最小。\n",
    "```\n",
    "\n",
    "## 解题方案\n",
    "\n",
    "> 思路 1\n",
    "\n",
    "使用 动态规划 ，我们考虑到，一个位于 [i][j] 位置的格子，到达它的路径只有 2 条：1 条是从上到下到达这个格子（[i-1][j] -> [i][j]），另一条是从左到右到达这个格子（[i][j-1] -> [i][j]）。我们只需要关注这两条路径中，哪条路径的路径和最小就好了。 另外可以参考 072.编辑距离 。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def minPathSum(self, grid):\n",
    "        \"\"\"\n",
    "        :type grid: List[List[int]]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        dp = grid.copy()\n",
    "        for i in range(1, n):\n",
    "            dp[0][i] = dp[0][i-1] + grid[0][i]\n",
    "        for i in range(1, m):\n",
    "            dp[i][0] = dp[i-1][0] + grid[i][0]\n",
    "        for i in range(1, m):\n",
    "            for j in range(1, n):\n",
    "                dp[i][j] = min(dp[i][j-1] + grid[i][j], dp[i-1][j] + grid[i][j])\n",
    "        return dp[m-1][n-1]\n",
    "    \n",
    "s = Solution()\n",
    "grid = [\n",
    "  [1,3,1],\n",
    "  [1,5,1],\n",
    "  [4,2,1]\n",
    "]\n",
    "print(s.minPathSum(grid))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的版本是我最初写成的，后来听大佬们说，尽量减少 for 循环的个数。改成下面的最终版本，已经 AC 。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def minPathSum(self, grid):\n",
    "        \"\"\"\n",
    "        :type grid: List[List[int]]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not grid or len(grid) == 0:\n",
    "            return 0\n",
    "        row = len(grid)\n",
    "        col = len(grid[0]) if row else 0\n",
    "        dp = [[0 for j in range(col)] for i in range(row)]\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if i > 0 and j > 0:\n",
    "                    dp[i][j] = min(dp[i-1][j]+grid[i][j], dp[i][j-1]+grid[i][j])\n",
    "                elif i > 0 and j == 0:\n",
    "                    dp[i][j] = sum([grid[k][0] for k in range(i+1)])\n",
    "                elif i == 0 and j > 0:\n",
    "                    dp[i][j] = sum([grid[0][k] for k in range(j+1)])\n",
    "                else:\n",
    "                    dp[i][j] = grid[0][0]\n",
    "        return dp[-1][-1]\n",
    "    \n",
    "s = Solution()\n",
    "grid = [\n",
    "  [1,3,1],\n",
    "  [1,5,1],\n",
    "  [4,2,1]\n",
    "]\n",
    "print(s.minPathSum(grid))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
